package org.cytoscape.myapp.cytoscape_app_v1.internal.UI;

import static org.cytoscape.work.ServiceProperties.ID;
import static org.cytoscape.work.ServiceProperties.IN_TOOL_BAR;
import static org.cytoscape.work.ServiceProperties.MENU_GRAVITY;
import static org.cytoscape.work.ServiceProperties.PREFERRED_MENU;
import static org.cytoscape.work.ServiceProperties.TITLE;
import static org.cytoscape.work.ServiceProperties.TOOL_BAR_GRAVITY;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.swing.CyMenuItem;
import org.cytoscape.application.swing.CyNodeViewContextMenuFactory;
import org.cytoscape.event.CyEventHelper;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.model.CyEdge.Type;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.CyNode;
import org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.CreateSubnetworkTask;
import org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.LayoutDefault;
import org.cytoscape.view.layout.CyLayoutAlgorithmManager;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.View;
import org.cytoscape.work.TaskIterator;
import org.cytoscape.work.TaskManager;
import org.cytoscape.work.TaskMonitor;




public class RightClickMenu implements CyNodeViewContextMenuFactory{

	 private Properties settingsProps;
	 private CreateSubnetworkTask task;
	 private TaskManager taskManager;
	 private CyGroupManager groupmanager;


	 public RightClickMenu(Properties settingsProps, CreateSubnetworkTask task, 
			 					TaskManager taskManager, CyGroupManager groupmanager){
		 this.settingsProps =  settingsProps;
		 this.task = task;
		 this.taskManager = taskManager;
		 this.groupmanager = groupmanager;
	 }

	public CyMenuItem createMenuItem(final CyNetworkView netView, final View<CyNode> nodeView)
	{
		JMenuItem menuItem = new JMenuItem(settingsProps.getProperty(TITLE));	
		if(groupmanager.isGroup(nodeView.getModel(), netView.getModel()))
		{
			menuItem.setEnabled(true);
			this.task.setNodeParent(nodeView.getModel().getSUID());
			menuItem.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent event)
				{
					taskManager.execute(new TaskIterator(task));
				}
			});
		}
		else
			menuItem.setEnabled(false);
		
		float gravity =  Float.valueOf(settingsProps.getProperty(MENU_GRAVITY));
		CyMenuItem addAsSource = new CyMenuItem(menuItem, gravity);
		return addAsSource;
		
	}
}




